#include <bits/stdc++.h>
using namespace std;
int main(){
    priority_queue<int,vector<int>,greater<int> >q;
    int n,num;
    cin>>n;
    for (int i=0;i<n;i++){
        cin>>num;
        q.push(num);
    }
    int sum=0;
    while(q.size()!=1){
        int x=l.top();
        q.pop();
        int y=l.top();
        q.pop();
        sum+=(x+y);
        q.push(x+y);
    }
    printf("%d\n",sum);
    return 0;
}
